﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using NHibernate;
using hbse.Entities;
using hbse.Service;
using hbse.DTO;

namespace hbse
{
    public partial class Usaddorder : UserControl
    {
        private TabItem newTab;
        private DevComponents.DotNetBar.TabControl tabControl2;
        private ISession session;
        private Users users;
        private AddOrderService addorderService = new AddOrderService();
        private OrderService orderService = new OrderService();
        private Orders order;
        private UntilService untilService = new UntilService();
        private SampleFolderMService sampleFolderMService = new SampleFolderMService();

        private BarTender.ApplicationClass btApp;
        private BarTender.Format btformat;

        public Usaddorder(DevComponents.DotNetBar.TabControl _tabControl2, TabItem _newTab, ISession _session, Users _users)
        {
            InitializeComponent();
            newTab = _newTab;
            tabControl2 = _tabControl2;
            session = _session;
            users = _users;

            try
            {
                btApp = new BarTender.ApplicationClass();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<ProductDTOAddOrder> productDTOAddOrders = addorderService.searchByCode(txtSearch.Text.Trim(), session);
            if (productDTOAddOrders.Count > 0)
            {
                dgvProduct.DataSource = productDTOAddOrders;
                FormatdgvProcuct();
            }
            else {
                dgvProduct.DataSource = null;
                FormatdgvProcuct();
                txtProductCode.Text = "";
                txtSaleOrder.Text = "";
                txtMrp.Text = "";
                txtQtyOrder.Text = "";
                txtFGStock.Text = "";
                txtQty.Text = "";
                txtPrice.Text = "";
                txtBarcode.Text = "";
            }
            
    

        }

        private void FormatdgvProcuct()
        {
            if (dgvProduct.RowCount > 0)
            {
                dgvProduct.Columns["ProductCode"].HeaderText = "ProductCode";
                dgvProduct.Columns["CustomerCode"].HeaderText = "CustomerCode";

                dgvProduct.Columns["Id"].Visible = false;
               
            }
        }

        private void dgvProduct_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }
            //AddOrderDTO addOrderDTO = addorderService.getByProductId(dgvProduct.Rows[e.RowIndex].Cells["ProductCode"].Value.ToString(), session);
            txtProductCode.Text = dgvProduct.Rows[e.RowIndex].Cells["ProductCode"].Value.ToString();
            //txtSaleOrder.Text = addOrderDTO.SaleOrder;
            //txtMrp.Text = addOrderDTO.MRPNo;
            //txtQtyOrder.Text = addOrderDTO.QtyOrder;
            //txtFGStock.Text = addOrderDTO.FGStock;
            //txtQty.Text = addOrderDTO.Qty;
            //txtPrice.Text = addOrderDTO.Price;
            //txtBarcode.Text = addOrderDTO.Barcode;

            //order = untilService.getOrderNotDTOBySaleOrder(addOrderDTO.SaleOrder, session);


        }

        private void cmdprint_Click(object sender, EventArgs e)
        {
            if (txtProductCode.Text.Trim() == "")
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("กรุณาเลือกข้อมูลก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtProductCode.Focus();
                return;
            }

           // var vorder = session.CreateQuery("from Orders o where o.Id=1200").Future<Orders>();

            order = new Orders();

            Product product = sampleFolderMService.getByCode(txtProductCode.Text.Trim(), session);

            order.Product = product;
            order.SaleOrder = txtSaleOrder.Text.Trim();
            order.MrpNo = txtMrp.Text.Trim();
            order.QtyOrder = txtQtyOrder.Text.Trim();
            order.FgStock = txtFGStock.Text.Trim();
            order.QtyProduction = txtQty.Text.Trim();
            order.Price = txtPrice.Text.Trim();
            order.Barcode = txtBarcode.Text.Trim();
            order.Users = users;
            order.AddDate = DateTime.Now;
            order.Status = "1";



            int addresult = orderService.addOrder(order, session);
            if (addresult > -1)
            {
                Console.WriteLine("Add Complete");
               // MessageBox.Show("บันทึกข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                Console.WriteLine("Add Not Complete");
               // MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้ !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

           

            List<OrderPrint> orderPrints = new List<OrderPrint>();
            OrderPrint orderPrint;
            orderPrint = new OrderPrint();
            orderPrint.Orders = order;
            orderPrints.Add(orderPrint);

            // print barcode 

            try {

                // ไม่มีลูก
                string path = Application.StartupPath + "\\AddOrder.btw";

                btApp.Visible = false;
                btformat = btApp.Formats.Open(path, true, "");

                // barcode 
                btformat.SetNamedSubStringValue("productcode", order.Product.ProductCode);
                btformat.SetNamedSubStringValue("saleorder", order.SaleOrder);
                btformat.SetNamedSubStringValue("barcode", "1"+ StringExt.StringFromLeft(order.Barcode,3));

                btformat.PrintOut(false, false);

             }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                btformat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
            }   



            order.OrderPrints = orderPrints;

            int updateresult = orderService.updateOrder(order, session);
            if (updateresult > -1)
            {
                Console.WriteLine("update Complete");
                MessageBox.Show("บันทึกข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                Console.WriteLine("update Not Complete");
                MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้ !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }



        }

    }
}
